import axios from "axios";
import { ElMessage } from "element-plus";


// 创建axios实例
const request = axios.create({
    
    baseURL: process.env.VUE_APP_BASE_DOMAIN+process.env.VUE_APP_BASE_URL,
    timeout: 180000,

});
// 给实例添加请求拦截器
request.interceptors.request.use((config) => {

    // config配置对象，headers请求头在里面

    return config;
});


request.interceptors.response.use((response) => {
    // 成功回调
    const contentType=response.headers["content-type"];
    //这样写为了兼容下载
    // console.log(response);
    if(contentType?.includes("application/octet-stream")){
        return response;
    }else{
        return response.data;

    }

}, (error) => {
    // 失败回调
    let message = "";
    const status = error.response.status;
    switch (status) {
        case 401:
            message = "token过期"
            break;
        case 403:
            message = "无权访问"
            break;
        case 404:
            message = "请求地址错误"
            break;
        case 500:
            message = "服务器错误"
            break;
        default:
            message = "网络错误"
            break;
    }
    ElMessage({
        type: 'error',
        message
    });

    return Promise.reject(error); // throw new Error("error");相当于这句话，抛出错误

});

export default request;